{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "0_WhatIsColab.ipynb",
      "version": "0.3.2",
      "provenance": [],
      "collapsed_sections": [],
      "toc_visible": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "oT30_0wOz5-P",
        "colab_type": "text"
      },
      "source": [
        "<!--COURSE_INFORMATION-->\n",
        "<img align=\"left\" style=\"padding-right:10px;\" src=\"https://sitejerk.com/images/google-earth-logo-png-5.png\" width=5% >\n",
        "<img align=\"right\" style=\"padding-left:10px;\" src=\"https://colab.research.google.com/img/colab_favicon_256px.png\" width=6% >\n",
        "\n",
        "\n",
        ">> *This notebook is part of the free course [EEwPython](https://colab.research.google.com/github/csaybar/EEwPython/blob/master/index.ipynb); the content is available [on GitHub](https://github.com/csaybar/EEwPython)* and released under the [Apache 2.0 License](https://www.gnu.org/licenses/gpl-3.0.en.html). 99% of this material has been adapted from [Google Earth Engine Guides](https://developers.google.com/earth-engine/)."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Y1EZ7wfkIv-a",
        "colab_type": "text"
      },
      "source": [
        "<!--NAVIGATION-->\n",
        "| [Contents](index.ipynb) | [Image](2_eeImage.ipynb) >\n",
        "\n",
        "<a href=\"https://colab.research.google.com/github/csaybar/EEwPython/blob/master/1_Introduction.ipynb\"><img align=\"left\" src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open in Colab\" title=\"Open and Execute in Google Colaboratory\"></a>\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "5AkUqloUQpXv",
        "colab_type": "text"
      },
      "source": [
        "<center>\n",
        "<h1>Google Earth Engine with Python </h1>\n",
        "<h2> Developer's Guide </h2>\n",
        "</center>\n",
        "<h2> Topics:</h2>\n",
        "\n",
        "\n",
        "1. What is colab?\n",
        "2. Explore colab for yourself\n",
        "3. What is Google Earth Engine?\n",
        "4. Why colab with python instead of code editor (Javascript)?\n",
        "5. Connecting GEE with other Google Services\n",
        "6. Hello World!\n",
        "7. Fundamental Earth Engine data structures\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "4DYheup28Y52",
        "colab_type": "code",
        "cellView": "form",
        "outputId": "3e526b21-908a-4c61-fcee-000887f32e7b",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 335
        }
      },
      "source": [
        "#@title 1. What is Colab?\n",
        "#@markdown [Colaboratory](https://research.google.com/colaboratory/faq.html) is a Google research project created to help disseminate machine learning education and research. It's a Jupyter notebook environment that requires **no setup to use and runs entirely in the cloud**. Colab runs over a Virtual Machine with Ubuntu 18.04, for activate the GPU and TPU support navigate for the **menu bar** as follow:\n",
        "#@markdown <center><b> Runtine ---> Change run time ---> Hardware accelarator </b></center>\n",
        "from IPython.display import HTML\n",
        "HTML('<center><iframe width=\"560\" height=\"315\" src=\"https://www.youtube.com/embed/inN8seMm7UI\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen></iframe></center>')"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<center><iframe width=\"560\" height=\"315\" src=\"https://www.youtube.com/embed/inN8seMm7UI\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen></iframe></center>"
            ],
            "text/plain": [
              "<IPython.core.display.HTML object>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 2
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "7m2n_2rW5t5V",
        "colab_type": "text"
      },
      "source": [
        "## 2. Explore colab for yourself!"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "A6AH7EXG8_in",
        "colab_type": "text"
      },
      "source": [
        "Note that you can communicate with the *bash console* prepending an `!` to the code."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "vPLUOy7D6El1",
        "colab_type": "code",
        "outputId": "17a473d3-4de0-4ff3-fcde-387a63fdcd1b",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 308
        }
      },
      "source": [
        "#check the SO\n",
        "!cat /etc/*release"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "DISTRIB_ID=Ubuntu\n",
            "DISTRIB_RELEASE=18.04\n",
            "DISTRIB_CODENAME=bionic\n",
            "DISTRIB_DESCRIPTION=\"Ubuntu 18.04.2 LTS\"\n",
            "NAME=\"Ubuntu\"\n",
            "VERSION=\"18.04.2 LTS (Bionic Beaver)\"\n",
            "ID=ubuntu\n",
            "ID_LIKE=debian\n",
            "PRETTY_NAME=\"Ubuntu 18.04.2 LTS\"\n",
            "VERSION_ID=\"18.04\"\n",
            "HOME_URL=\"https://www.ubuntu.com/\"\n",
            "SUPPORT_URL=\"https://help.ubuntu.com/\"\n",
            "BUG_REPORT_URL=\"https://bugs.launchpad.net/ubuntu/\"\n",
            "PRIVACY_POLICY_URL=\"https://www.ubuntu.com/legal/terms-and-policies/privacy-policy\"\n",
            "VERSION_CODENAME=bionic\n",
            "UBUNTU_CODENAME=bionic\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "0xKgYVk7YPlz",
        "colab_type": "code",
        "outputId": "edf530f7-0eac-44d9-c54d-de35662585f1",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 445
        }
      },
      "source": [
        "#How many cpu are there?\n",
        "!lscpu"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Architecture:        x86_64\n",
            "CPU op-mode(s):      32-bit, 64-bit\n",
            "Byte Order:          Little Endian\n",
            "CPU(s):              2\n",
            "On-line CPU(s) list: 0,1\n",
            "Thread(s) per core:  2\n",
            "Core(s) per socket:  1\n",
            "Socket(s):           1\n",
            "NUMA node(s):        1\n",
            "Vendor ID:           GenuineIntel\n",
            "CPU family:          6\n",
            "Model:               79\n",
            "Model name:          Intel(R) Xeon(R) CPU @ 2.20GHz\n",
            "Stepping:            0\n",
            "CPU MHz:             2200.000\n",
            "BogoMIPS:            4400.00\n",
            "Hypervisor vendor:   KVM\n",
            "Virtualization type: full\n",
            "L1d cache:           32K\n",
            "L1i cache:           32K\n",
            "L2 cache:            256K\n",
            "L3 cache:            56320K\n",
            "NUMA node0 CPU(s):   0,1\n",
            "Flags:               fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc rep_good nopl xtopology nonstop_tsc cpuid pni pclmulqdq ssse3 fma cx16 sse4_1 sse4_2 x2apic movbe popcnt aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch pti ssbd ibrs ibpb stibp fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms rtm rdseed adx smap xsaveopt arat arch_capabilities\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "RgXgiM4mYSql",
        "colab_type": "code",
        "outputId": "732e31db-8bdf-4f39-92f2-9d8c68a28bb8",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        }
      },
      "source": [
        "# How much RAM does it have?\n",
        "!grep MemTotal /proc/meminfo"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "MemTotal:       13335212 kB\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "vNaZr_fU8rkU",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# Check the default libraries installed\n",
        "!pip3 freeze"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Asixz4C0T02x",
        "colab_type": "text"
      },
      "source": [
        "## 3. What is Google Earth Engine?\n",
        "Google Earth Engine is a computing platform that allows users to run geospatial analysis on Google's infrastructure. There are several ways to interact with the platform:\n",
        "\n",
        "  - [Explorer](https://explorer.earthengine.google.com/)\n",
        "  - [Code Editor](https://code.earthengine.google.com/)  \n",
        "  - [Javascript wrapper library](https://github.com/google/earthengine-api/tree/master/javascript)\n",
        "  - [**Python wrapper library**](https://github.com/google/earthengine-api/tree/master/python)\n",
        "\n",
        "This course is focus in the last one, you can use the **Python wrapper lbrary** to build [custom applications](https://github.com/google/earthengine-api/tree/master/demos) and to develop Earth Engine code locally. Continue the *EEwPython* for an overview of this, or visit the **[Earth Engine’s Developer Guide](https://developers.google.com/earth-engine/getstarted)**  or the demos hosted in [GitHub](https://github.com/google/earthengine-api) for an in-depth guide.\n",
        "\n",
        "The purpose of Earth Engine is to:\n",
        "\n",
        "- Perform highly-interactive algorithm development at global scale\n",
        "\n",
        "- Push the edge of the envelope for big data in remote sensing\n",
        "\n",
        "- Enable high-impact, data-driven science\n",
        "\n",
        "- Make substantive progress on global challenges that involve large geospatial datasets\n",
        "\n",
        "### Components:\n",
        "\n",
        "The main components of Earth Engine are:\n",
        "\n",
        "- [**Datasets**](https://developers.google.com/earth-engine/datasets/): A petabyte-scale archive of publicly available remotely sensed imagery and other data. Explore the data catalog.\n",
        "\n",
        "- [**Compute power**](https://reader.elsevier.com/reader/sd/pii/S0034425717302900?token=2AC58EA43ED5306A13DA0E6AF50A5F286D59B455B45F52F9932FE71845E5C75C706C78E079BC3769B23B256612A225D0): Google’s computational infrastructure optimized for parallel processing of geospatial data.\n",
        "\n",
        "- **APIs**: APIs for JavaScript and Python (hosted on GitHub) for making requests to the Earth Engine servers. These docs focus on JavaScript; (Get started guide for the Javascript API). For Python, see the Python install guide and the Python examples in the Earth Engine GitHub repository.\n",
        "\n",
        "- **Code Editor**: An online Integrated Development Environment (IDE) for rapid prototyping and visualization of complex spatial analyses using the Javascript API. Explore the Code Editor docs."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "QgDV721Tg2kD",
        "colab_type": "code",
        "cellView": "form",
        "outputId": "5b63ae15-6740-40ec-919a-2ce857bfb2d9",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 335
        }
      },
      "source": [
        "#@title ### Meet Earth Engine\n",
        "from IPython.display import HTML\n",
        "HTML('<center><iframe width=\"560\" height=\"315\" src=\"https://www.youtube.com/embed/gKGOeTFHnKY\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen></iframe></center>')"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<center><iframe width=\"560\" height=\"315\" src=\"https://www.youtube.com/embed/gKGOeTFHnKY\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen></iframe></center>"
            ],
            "text/plain": [
              "<IPython.core.display.HTML object>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 4
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "_jMW3xWUg6Gr",
        "colab_type": "text"
      },
      "source": [
        "\n",
        "### Note about access and API stability\n",
        "The Earth Engine API and advanced Earth Engine functionality are experimental and subject to change. Access is limited and requires requesting access via our form."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "9CRoDhQNiHC8",
        "colab_type": "text"
      },
      "source": [
        "## 4. Why colab with python instead of code editor (Javascript)?\n",
        "\n",
        "A short comparison thanks to [Tyler Erickson presentation](https://docs.google.com/presentation/d/1MVVeyCdm-FrMVRPop6wB3iyd85TAlwB-F9ygTQZ8S1w/pub?slide=id.g1e419debf0_1_205).\n",
        "\n",
        "**Javascript** | **Python**\n",
        "------------|-----------------\n",
        "- Easy to get started | - Easy to share code between scripts.\n",
        "- Trivial  to share scripts | - Easier transition  to a web application.\n",
        "- Built in authentication | - Allows for series of calls  to Earth Engine.\n",
        "- Limited input/output funcionality | - Many, many plotting options\n",
        "- Can't share code between scripts | - Some assembly (& maintenance) required!.\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "3fvjtgedmrxq",
        "colab_type": "text"
      },
      "source": [
        "##  5. Connecting GEE with other Google Services\n",
        "\n",
        "\n",
        "The Earth Engine Python API and command line tools can be installed using Python's pip package installation tool.\n",
        "\n",
        "Psss!: The following notebook cell line is starts with ! to indicate that a shell command should be invoked."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "_LSeUMzmoQpw",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "!pip install earthengine-api"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "iC8wVueFoT85",
        "colab_type": "text"
      },
      "source": [
        "### Authentication \n",
        "\n",
        "To read/write from a Google Drive or Google Cloud Storage bucket to which you have access, it's necessary to authenticate (as yourself). You'll also need to authenticate as yourself with Earth Engine, so that you'll have access to your scripts, assets, etc.\n",
        "\n",
        "#### Authenticate to Earth Engine\n",
        "\n",
        "In order to access Earth Engine, signup at [signup.earthengine.google.com](https://signup.earthengine.google.com/).\n",
        "\n",
        "Identify yourself to Google Cloud, so you have access to storage and other resources. When you run the code below, it will display a link in the output to an authentication page in your browser. Follow the link to a page that will let you grant permission to the Google Earth Engine to access your resources. Copy the code from the permissions page back into this notebook and press return to complete the process.\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "MeEs5x5uoevg",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "!earthengine authenticate"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "SQ2O6uE2uRgL",
        "colab_type": "text"
      },
      "source": [
        "#### Authenticate to Google Drive\n",
        "\n",
        "Authenticate to **Google Drive** on  the same way you did to the Earth Engine."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "SGq1N9ljuVbl",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "from google.colab import drive\n",
        "drive.mount('/content/drive')"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "cxGc4qGJuY1K",
        "colab_type": "text"
      },
      "source": [
        "#### Authenticate to Google Cloud\n",
        "\n",
        "Authenticate to **Google Cloud** on  the same way you did to the Earth Engine.\n"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "RUsQ8LJQuefL",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "from google.colab import auth\n",
        "auth.authenticate_user()"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "NHrBAom4whXN",
        "colab_type": "text"
      },
      "source": [
        " Once you have authorized access (just Earth Engine auth is required). Initialize your connection as shown the cell below."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "5F0WYN_8xECE",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# Import the Earth Engine API and initialize it.\n",
        "import ee\n",
        "ee.Initialize()"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "smUsOFYLy3J5",
        "colab_type": "text"
      },
      "source": [
        "## 6. Hello World!\n",
        "\n",
        "\n",
        "- **assetID**: Is the key to identify a specific file in [GEE datasets](https://developers.google.com/earth-engine/datasets/catalog/USGS_SRTMGL1_003).\n",
        "- **ee.Image('assetID')**: In GEE raster data are represented as Image objects (see the next topic). \n",
        "- **ee.Image.clip(ee.Geometry.____)**: Is a ee.Image method used to crop spatially all the bands in Images.\n",
        "- **folium**: bridge between Python and [Leaflet](https://leafletjs.com/)\n"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "GeMYFEvKy43n",
        "colab_type": "code",
        "outputId": "208a7be4-8ba5-4e03-f220-c53c6096cc33",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 559
        }
      },
      "source": [
        "import folium\n",
        "\n",
        "Peru = ee.Image(\"USGS/SRTMGL1_003\")\\\n",
        "         .clip(ee.Geometry.Rectangle(-85, -20, -65,0))\n",
        "\n",
        "# Define the URL format used for Earth Engine generated map tiles.\n",
        "EE_TILES = 'https://earthengine.googleapis.com/map/{mapid}/{{z}}/{{x}}/{{y}}?token={token}'\n",
        "\n",
        "# Use folium to visualize the imagery.\n",
        "mapid = Peru.getMapId({'pallete': ['black', 'white'], 'min': 0, 'max': 3000})\n",
        "\n",
        "map = folium.Map(location=[-10, -75],zoom_start=5)\n",
        "folium.TileLayer(\n",
        "    tiles=EE_TILES.format(**mapid),\n",
        "    attr='Google Earth Engine',\n",
        "    overlay=True,\n",
        "    name='median composite',\n",
        "  ).add_to(map)\n",
        "map.add_child(folium.LayerControl())\n",
        "map"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div style=\"width:100%;\"><div style=\"position:relative;width:100%;height:0;padding-bottom:60%;\"><iframe src=\"data:text/html;charset=utf-8;base64,PCFET0NUWVBFIGh0bWw+CjxoZWFkPiAgICAKICAgIDxtZXRhIGh0dHAtZXF1aXY9ImNvbnRlbnQtdHlwZSIgY29udGVudD0idGV4dC9odG1sOyBjaGFyc2V0PVVURi04IiAvPgogICAgPHNjcmlwdD5MX1BSRUZFUl9DQU5WQVM9ZmFsc2U7IExfTk9fVE9VQ0g9ZmFsc2U7IExfRElTQUJMRV8zRD1mYWxzZTs8L3NjcmlwdD4KICAgIDxzY3JpcHQgc3JjPSJodHRwczovL2Nkbi5qc2RlbGl2ci5uZXQvbnBtL2xlYWZsZXRAMS40LjAvZGlzdC9sZWFmbGV0LmpzIj48L3NjcmlwdD4KICAgIDxzY3JpcHQgc3JjPSJodHRwczovL2NvZGUuanF1ZXJ5LmNvbS9qcXVlcnktMS4xMi40Lm1pbi5qcyI+PC9zY3JpcHQ+CiAgICA8c2NyaXB0IHNyYz0iaHR0cHM6Ly9tYXhjZG4uYm9vdHN0cmFwY2RuLmNvbS9ib290c3RyYXAvMy4yLjAvanMvYm9vdHN0cmFwLm1pbi5qcyI+PC9zY3J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DQ2MjkxNmM4MzFhNDQpOwogICAgICAgICAgICAKICAgICAgICAKPC9zY3JpcHQ+\" style=\"position:absolute;width:100%;height:100%;left:0;top:0;border:none !important;\" allowfullscreen webkitallowfullscreen mozallowfullscreen></iframe></div></div>"
            ],
            "text/plain": [
              "<folium.folium.Map at 0x7f4cd60a9710>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 16
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "7QhIzsv01DU5",
        "colab_type": "text"
      },
      "source": [
        "## 7. Fundamental Earth Engine data structures\n",
        "\n",
        "The two most fundamental geographic data structures in Earth Engine are **`Image`** and **`Feature`** corresponding to raster and vector data types, respectively. Images are composed of bands and a dictionary of properties. Features are composed of a **`Geometry`** and a dictionary of properties. A stack of images (e.g. an image time series) is handled by an **`ImageCollection`**. A collection of features is handled by a  **`FeatureCollection`**. Other fundamental data structures in Earth Engine include:\n",
        "\n",
        "- `Dictionary`\n",
        "- `List`\n",
        "- `Array`\n",
        "- `Geometry`\n",
        "- `Date`\n",
        "- `Number`\n",
        "- `String`.\n",
        "\n",
        "It is important to remember that these are all **server-side objects (or containers)**. That is, your client browser does not know anything about the objects in your script unless you explicitly request information about them. That request triggers a message being passed from Google to the Python API. If the message is large, there will be a corresponding slow down. Existing two ways to inspect **Earth Engine containers**  in Python console. The first one, `print()` that return the  petition (as a JSON) to the server and `*.getInfo()` that returns the contents of the `container`.  The next section summaries all the Data Structures mentioned above.\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "LzAFvH1p4L_C",
        "colab_type": "text"
      },
      "source": [
        "#### 7.1. Strings\n",
        "\n",
        "Define a Python string, then put it into the **`ee.String()`** container to be sent to Earth Engine:"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "NDvyLLcw16Dw",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# Define a string, then put it into an EE container.\n",
        "aString = 'To the cloud!'\n",
        "eeString = ee.String(aString)\n",
        "print('Where to?', eeString)\n",
        "eeString"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "zgmudfl-2Ek6",
        "colab_type": "text"
      },
      "source": [
        "Think of **`ee.Thing`** as a container for a thing that exists on the server. In this example, the string is defined first, then put into the container. \n",
        "\n",
        "### 7.2. Numbers\n",
        "\n",
        "Use **`ee.Number()`** to create number objects on the server."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "w0VGwFim3Ugp",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# Define a number that exists on the server.\n",
        "import numpy as np\n",
        "serverNumber = ee.Number(np.e)\n",
        "print('e=', serverNumber)\n",
        "serverNumber"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "-IFAt2YT3Ytx",
        "colab_type": "text"
      },
      "source": [
        "### 7.3. Lists\n",
        "\n",
        "To make a Python list into an **`ee.List`** object on the server, you can put a Python literal into a container as with numbers and strings. Earth Engine also provides server-side convenience methods for making sequences of numbers.\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "J1kzgip03XPb",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# Make a sequence the hard way.\n",
        "eeList = ee.List([1, 2, 3, 4, 5])\n",
        "\n",
        "# Make a sequence the easy way!\n",
        "sequence = ee.List.sequence(1, 5);\n",
        "\n",
        "print('Sequence:', sequence)\n",
        "print('Opening the container:', sequence.getInfo())"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "yXsITlrg3c4U",
        "colab_type": "text"
      },
      "source": [
        "### 7.4. Dictionaries\n",
        "\n",
        "You can construct an Earth Engine `Dictionary` from a Python object, as with strings, numbers and lists. At construction time, you can use Python functionality to initialize the Earth Engine object. In this case an ee.Dictionary is constructed directly from a Python literal object:"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "p808bk_F3dTZ",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "dictionary = ee.Dictionary({\n",
        "  'e': np.e,\n",
        "  'pi': np.pi,\n",
        "  'phi': (1 + np.sqrt(5))/2\n",
        "})\n",
        "\n",
        "# Get some values from the dictionary.\n",
        "print('Euler:', dictionary.get('e').getInfo())\n",
        "print('Pi:', dictionary.get('pi').getInfo())\n",
        "print('Golden ratio:', dictionary.get('phi').getInfo())\n",
        "\n",
        "# Get all the keys:\n",
        "print('Keys: ', dictionary.keys().getInfo())"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "VapD_lKe3h1h",
        "colab_type": "text"
      },
      "source": [
        "In this example, observe that once you have an `ee.Dictionary`, you must use methods on the `ee.Dictionary` to get values. Specifically, get(key) returns the value associated with key. Since the type of object returned by `get()` could be be anything, if you're going to do anything to the object other then print it, you need to cast it to the right type. Also note that the `keys()` method returns an List."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "O08_uhb63puh",
        "colab_type": "text"
      },
      "source": [
        "### 7.5. Dates\n",
        "\n",
        "Date objects are the way Earth Engine represents time. As in the previous examples, it is important to distinguish between a Python Date object and an Earth Engine `ee.Date` object.\n",
        "\n",
        "A Date object can be construct from:\n",
        "  - A string.\n",
        "  - Python [datetime](https://docs.python.org/3/library/datetime.html) object.\n",
        "  - Static methods provided by the ee.Date class.\n",
        "  \n",
        "The next examples illustrates the construction of dates from these three forms."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "8QOrLUcP3icW",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# Define a date in Earth Engine.\n",
        "date = ee.Date('2015-12-31')\n",
        "print('Date:', date)\n",
        "print('Date:', date.getInfo())"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "QSLwbOtd3uuE",
        "colab_type": "text"
      },
      "source": [
        "In earth engine the date is representing with timestamps since midnight on January 1, 1970 considering an in milliseconds timestep . "
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "z7ZMj2FI3wc2",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# Get the current time using the JavaScript Date.now() method.\n",
        "import datetime as dt\n",
        "now = dt.datetime.now()\n",
        "print('Milliseconds since January 1, 1970', now)\n",
        "\n",
        "# Initialize an ee.Date object.\n",
        "eeNow = ee.Date(now)\n",
        "print('Now:', eeNow)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "ya02lB5K3zAY",
        "colab_type": "text"
      },
      "source": [
        "Arguments to Earth Engine methods can be passed in order, for example to create an `ee.Date` from year, month and day, you can pass parameters of the `fromYMD()` static method in the order year, month, day:\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "rm8Ae53731KW",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "aDate = ee.Date.fromYMD(2017, 1, 13)\n",
        "print('aDate:', aDate)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "wxOiCbzJg9k8",
        "colab_type": "text"
      },
      "source": [
        "<!--NAVIGATION-->\n",
        "| [Contents](index.ipynb) | [Image](2_eeImage.ipynb) >\n",
        "\n",
        "<a href=\"https://colab.research.google.com/github/csaybar/EEwPython/blob/master/1_Introduction.ipynb\"><img align=\"left\" src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open in Colab\" title=\"Open and Execute in Google Colaboratory\"></a>\n"
      ]
    }
  ]
}